extends Node2D

# 支撑属性
var width: float = 12.0
var color: Color = Color.WHITE
var move_speed: float = 10.0

# --- 新增：延伸动画相关变量 ---
var target_length: float = 0.0      # 目标长度（由主场景传入）
var growth_progress: float = 0.0    # 当前延伸进度 (0.0 -> 1.0)
var growth_speed: float = 2.0       # 延伸速度 (每秒增长的比例)
var is_growing: bool = true         # 是否正在延伸

@onready var line: Line2D = $SupportLine

func _ready():
	# 初始化时隐藏线条，等待延伸动画
	line.clear_points()
	line.width = width
	line.default_color = color

func setup(start_speed: float, required_length: float):
	""" 在主场景生成时调用，初始化速度与长度 """
	move_speed = start_speed
	target_length = required_length
	# 开始延伸动画
	is_growing = true
	growth_progress = 0.0

func _process(delta):
	if Translate.h:
		line.default_color = Color("8787bb")
	else:
		line.default_color = Color("ffffff")
	
	position.x -= move_speed * delta * 60
	
	# 2. 执行向下延伸动画
	if is_growing:
		growth_progress = min(growth_progress + growth_speed * delta, 1.0)
		draw_animated_support()
		
		# 延伸动画完成
		if growth_progress >= 1.0:
			is_growing = false
	
	# 3. 移出屏幕后删除
	if global_position.x < -300:
		queue_free()

func draw_animated_support():
	""" 根据当前生长进度绘制支撑 """
	line.clear_points()
	# 起点：桥连接点
	line.add_point(Vector2(0, 0))
	# 终点：根据当前进度计算
	var current_length = target_length * growth_progress
	line.add_point(Vector2(0, current_length))

# --- 修改：统一速度同步函数 ---
func update_speed(new_speed: float):
	""" 由主场景调用，强制同步速度 """
	# 立即更新速度，确保下一帧移动量就与桥身一致
	move_speed = new_speed
